<?php
namespace Wap\Controller;
use Common\Model\NoticeModel;
use Common\Model\PraiseModel;
use Common\Model\UserModel;
use Common\Model\UtilsModel;
use Wap\BaseController;
use Common\Model\CommentModel;
//use Common\Model\PraiseModel;
class CommentController extends BaseController {


    public function index(){
        die;
    }

    //评论显示默认评论
    public function defaultList(){
        $type = I('get.type',0,'intval');
        $id = I('get.id',0,'intval');
        $pageSize = I('get.pageSize',3,'intval');
        $page = I('get.page',1,'intval');
        if(!$type || !$id){
            $this->ajaxReturn(array('status'=>1, 'msg'=>'参数错误'));
        }
        $list=CommentModel::getCommentTop($type,$id,$pageSize);
        //emoji表情处理 手机版去掉
//        if($list) {
//            include WEB_ROOT . '/Application/Common/Common/emoji.php';
//            foreach ($list['list'] as $key => $val) {
//                $tmpStr = preg_replace("#(\\\ue[0-9a-f]{3})#ie","addslashes('\\1')",json_encode($val['content'])); //将emoji的unicode留下，其他不动
//                $list['list'][$key]['content'] = emoji_unified_to_html(json_decode($tmpStr));
//            }
//        }

        $this->smarty->assign('list',$list['list']);
        $this->smarty->assign('type',$type);

        $commentPraiseSwitch = true;
        $this->smarty->assign("commentPraiseSwitch",$commentPraiseSwitch);

        $html=$this->smarty->fetch('Comment/comment_list_item.html');

        $this->ajaxReturn(array('status'=>0, 'dataHTML'=>$html,'isPage'=>$list['nextPage']));
    }

    //评论显示默认评论
    public function comment(){
        $type=I('get.type',0,'int');
        $id=I('get.id',0,'int');
        //是否当日首次签到
        $uid = session('uid')+0;
        signAssignWap($uid,$this->smarty);

        $this->smarty->assign('title',C('SITE_NAME'));
        $this->smarty->assign('type',$type);
        $this->smarty->assign('id',$id);
        $this->smarty->display("Comment_v2/commentList.html");
    }

    //加载更多
    public function loadMore(){
        $page = I('get.page',2,'intval');
        $type = I('get.type',0,'intval');
        $id = I('get.id',0,'intval');
        if(!$id || !$type){
            $this->ajaxReturn(array('status'=>0, 'dataHTML'=>'没有评论'));
        }
        $data=CommentModel::getCommentList($type,$id,$page,8);
        //emoji表情处理 手机版去掉
//        if($data) {
//            include WEB_ROOT . '/Application/Common/Common/emoji.php';
//            foreach ($data['list'] as $key => $val) {
//                $tmpStr = preg_replace("#(\\\ue[0-9a-f]{3})#ie","addslashes('\\1')",json_encode($val['content'])); //将emoji的unicode留下，其他不动
//                $data['list'][$key]['content'] = emoji_unified_to_html(json_decode($tmpStr));
//            }
//        }

        $this->smarty->assign('type',$type);
        $commentPraiseSwitch = true;
        $this->smarty->assign("commentPraiseSwitch",$commentPraiseSwitch);
        $this->smarty->assign('list',$data['list']);
        $html = $this->smarty->fetch('Comment_v2/comment_list_item.html');
        $this->ajaxReturn(array('status'=>0, 'dataHTML'=>$html, 'isPage'=>$data['nextPage']));
    }

    //添加评论
    public function addComment(){

        $objType=I('post.type',0,'int');
        $objID=I('post.id',0,'int');
        $parentID=I('post.parentID',0,'int');//父级评论
        $content=I('post.content','','strip_tags');//
        $content=preg_replace('/@.*:/','',$content);

        if(!session('uid')){
            $this->ajaxReturn(array('status'=>99, 'msg'=>'请先登录', 'uri'=>U('User/login')."&uri=".urlencode(U('Comment/comment')."&type=".$objType."&id=".$objID) ));
        }

        if(!$objType || !$objID){
            $this->ajaxReturn(array('status'=>1, 'msg'=>'参数错误'));
        }

        if(true !== $maskStr = checkCleanWord($content) ){
            $this->ajaxReturn(array('status'=>1, 'msg'=>'评论内容中不能包含'.$maskStr));
            return;
        }
        $uinfo = array(
            'uid'=>session('uid'),
            'nickname'=>session('nickname')
        );

        $data = CommentModel::commentNew($objType, $objID, $parentID, $content, $uinfo);//, array('parentId'=>$parentID, 'content'=>$content, 'type'=>$objType, 'id'=>$objID));
        $this->ajaxReturn($data);

    }

    //点赞评论
    public function praiseComment(){
        if(session('uid')+0<1) $this->ajaxReturn(array('status'=>99));
        $commentID = I("get.id",0,'int');
        $objType = I('get.type',0,'int');
        if($commentID == 0){
            $this->ajaxReturn(array('status'=>1,'msg'=>"参数错误"));
        }
        $commentPraiseType = OBJ_TYPE_COMMENT_ARTICLE;  //默认为 对文章的评论的点赞
        if($objType == OBJ_TYPE_ACTIVITY){
            $commentPraiseType = OBJ_TYPE_COMMENT_ACTIVITY;
        }
        $res = PraiseModel::praise($commentPraiseType,$commentID,session('uid'),session('nickname'),0);
        $this->ajaxReturn($res);
    }


}